<template>
  <div id="usermanage">
    <!--搜索表单开始-->
    <div class="search" style="margin-bottom: 20px">
      <el-input
        style="width: 240px"
        placeholder="请输入房源标题"
        v-model="title"
      ></el-input>
      <el-input
        style="width: 240px; margin-left: 5px"
        placeholder="请输入房源城市"
        v-model="city"
      ></el-input>
      <el-button
        style="margin-left: 5px"
        type="primary"
        @click="(pageNum = 1), load()"
        ><i class="el-icon-search"></i> 搜索</el-button
      >
      <el-button style="margin-left: 5px" type="warning" @click="reset"
        ><i class="el-icon-refresh"></i> 重置</el-button
      >
    </div>
    <!-- 搜索表单结束 -->
    <!-- 表格开始 -->
    <div class="housetable">
      <el-table stripe :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="房屋编号">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="标题">
                <span>{{ props.row.title }}</span>
              </el-form-item>
              <el-form-item label="租赁方式">
                <span>{{ props.row.type === 0 ? "整租" : "合租" }}</span>
              </el-form-item>
              <el-form-item label="房租价格">
                <span>{{ props.row.money }}</span>
              </el-form-item>
              <el-form-item label="城市">
                <span>{{ props.row.city }}</span>
              </el-form-item>
              <el-form-item label="详细地址">
                <span>{{ props.row.houseSite }}</span>
              </el-form-item>
              <el-form-item label="房屋面积">
                <span>{{ props.row.area }}</span>
              </el-form-item>
              <el-form-item label="所在楼层">
                <span>{{ props.row.floor }}</span>
              </el-form-item>
              <el-form-item label="总楼层">
                <span>{{ props.row.allfloor }}</span>
              </el-form-item>
              <el-form-item label="厕所数量">
                <span>{{ props.row.toilet }}</span>
              </el-form-item>
              <el-form-item label="厨房数量">
                <span>{{ props.row.kitchen }}</span>
              </el-form-item>
              <el-form-item label="客厅数量">
                <span>{{ props.row.living }}</span>
              </el-form-item>
              <el-form-item label="卧室数量">
                <span>{{ props.row.bedroom }}</span>
              </el-form-item>
              <el-form-item label="是否有电梯">
                <span>{{ props.row.elevator === false ? "否" : "是" }}</span>
              </el-form-item>
              <el-form-item label="房源描述">
                <span>{{ props.row.content }}</span>
              </el-form-item>
              <!-- 这个应该是好几张图片 -->
              <el-form-item label="房源图片">
                <div
                  class="houseimg"
                  v-for="(item, index) in props.row.houseImgs"
                  :key="index"
                >
                  <el-image
                    style="width: 100px; height: 100px"
                    :src="item"
                  ></el-image>
                </div>
              </el-form-item>
              <el-form-item label="创建时间">
                <span>{{ formatDate(props.row.createTime) }}</span>
              </el-form-item>
              <el-form-item label="更新时间">
                <span>{{ formatDate(props.row.updateTime) }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="房屋编号" prop="houseAccount">
        </el-table-column>
        <el-table-column label="房东账号" prop="ownerAccount">
        </el-table-column>
        <el-table-column label="是否在租" prop="rentIs">
          <template slot-scope="{ row }">
            {{ row.rentIs ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column label="租客账号" prop="rentAccount"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- <el-button
              size="mini"
              style="margin-right: 6px"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            > -->
            <el-popconfirm
              title="确定删除吗？"
              @confirm="handleDelete(scope.$index, scope.row)"
            >
              <el-button slot="reference" size="mini" type="danger"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; justify-content: center">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>


<script>
import request from "@/utils/request";
export default {
  name: "HelloWorld",
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 5,
      title: "",
      city: "",
      tableData: [],
    };
  },
  created() {
    this.load();
  },
  methods: {
    async load() {
      try {
        const res = await request.get("/house/page/admin", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            title: this.title,
            city: this.city,
          },
        });
        this.tableData = res.data.records;
        const rentAccountPromises = this.tableData.map((row) =>
          this.getUserAccount(row.rentId)
        );
        const ownerAccountPromises = this.tableData.map((row) =>
          this.getUserAccount(row.ownerId)
        );
        const houseImgPromises = this.tableData.map((row) =>
          this.getHouseImg(row.houseAccount)
        );
        const rentAccounts = await Promise.all(rentAccountPromises);
        const ownerAccounts = await Promise.all(ownerAccountPromises);
        const houseImgs = await Promise.all(houseImgPromises);
        for (let i = 0; i < this.tableData.length; i++) {
          this.$set(this.tableData[i], "rentAccount", rentAccounts[i]);
          this.$set(this.tableData[i], "ownerAccount", ownerAccounts[i]);
          this.$set(this.tableData[i], "houseImgs", houseImgs[i]);
          console.log(houseImgs[i]);
        }
        this.total = res.data.total;
      } catch (error) {
        console.log(error);
      }
    },
    async getHouseImg(houseAccount) {
      const res = await request.get("/file/imgurls/" + houseAccount);
      if (res.code === 200) {
        return res.data;
      } else {
        this.$notify.error(res.msg);
      }
    },
    async getUserAccount(id) {
      if (id != null) {
        try {
          const res = await request.get("/user/" + id);
          if (res.code === 200) {
            return res.data.userName;
          } else {
            this.$notify.error(res.msg);
            return "";
          }
        } catch (error) {
          console.error(error);
          this.$notify.error("获取用户账号失败");
          return "";
        }
      }
    },
    reset() {
      this.pageNum = 1;
      this.pageSize = 5;
      this.title = "";
      this.city = "";
      this.load();
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(row.id);
      request.delete("/house/" + row.id).then((res) => {
        if (res.code === 200) {
          this.$notify.success("删除成功");
          this.load();
        } else {
          this.$notify.error(res.msg);
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.load();
    },
    formatDate(timestamp) {
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const day = date.getDate();
      const hour = date.getHours();
      const minute = date.getMinutes();
      const second = date.getSeconds();
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
  },
};
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.search {
  width: 675px;
  margin-left: 80px;
}
.el-button--primary {
  color: #fff;
  background-color: rgb(0, 184, 224);
  border-color: rgb(0, 184, 224);
}
/* 表格样式开始 */
.housetable {
  width: 1050px;
  /* margin-left: 350px; */
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
/* 表格样式结束 */
.houseimg {
  float: left;
  width: 115px;
}
</style>